package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.qo.StatementItemQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public void removeByStatementId(Long statementId) {
        statementItemMapper.deleteByStatementId(statementId);
    }

    // 支付
    @Override
    public void payStatement(Long statementId) {
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            // 已支付
            throw new BusinessException("已支付,不能再次支付");
        }
        // 跟新结算单数据
        // ShiroUtils.getUserId():收款人ID
        statementService.updatePayStatement(statementId,Statement.STATUS_PAID, ShiroUtils.getUserId());
        // 回去预约单ID
        Long appointmentId = statement.getAppointmentId();
        if (appointmentId != null){
            // 说明结算单是预约单生成的
            // 修改预约单状态
            appointmentService.updateStatus(appointmentId,Appointment.STATUS_SETTLE);
        }
    }

    @Override
    public void saveItems(List<StatementItem> items) {
        // 获取最后一条数据 并删除
        StatementItem tempItem = items.remove(items.size() - 1);
        // 折扣金额
        BigDecimal disCountPrice = tempItem.getItemPrice();
        // 结算单状态为消费中才能结算
        // items里的StatementItem对象都封装了statementId
        Long statementId = tempItem.getStatementId();
        Statement statement = statementService.get(statementId);
        if (Statement.STATUS_PAID.equals(statement.getStatus())){
            // 支付状态
            throw new BusinessException("已支付的计算单不能保存明细");
        }
        // 插入明细单数据前先删除全数据
        statementItemMapper.deleteByStatementId(statementId);
        // 计算总金额 和总数量
        BigDecimal totalAmount = new BigDecimal("0.00");
        BigDecimal totalQuantity = new BigDecimal("0.00");
        for (StatementItem item : items) {
            // 把每个明细单添加到数据库中
            statementItemMapper.insert(item);
            // 总金额
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            // 总数量
            totalQuantity = totalQuantity.add(item.getItemQuantity());
        }
        // 如果折扣金额大于总金额
        if (disCountPrice.compareTo(totalAmount) > 0){
            throw new BusinessException("非法操作,折扣金额大于总金额");
        }
        // 更新服务单数据
        statementService.updateAmount(statementId,totalAmount,totalQuantity,disCountPrice);
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }
}
